package com.cakeclient;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.cakeclient.Model.Basket;
import com.cakeclient.Model.CatalogCategory;
import com.cakeclient.Model.ProductType;

import java.util.List;

public class ListviewClientCatalogAdapter extends ArrayAdapter<Object> {
    int resource;
    Context context;
    boolean changeColor = true;
    int myGrey = Color.rgb(231, 227, 231);
    int myBlue = Color.rgb(198,219,222);

    public ListviewClientCatalogAdapter(Context context, int textViewResourceId, List<Object> objects) {
        super(context, textViewResourceId, objects);
        this.resource = textViewResourceId;
        this.context = context;
    }

    // On override ArrayAdapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout objectView;

        // Récuperer l'order courant
        final Object object = getItem(position);


        // Inflate the catalog_detail_product
        if (convertView == null) {
            objectView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
            vi.inflate(resource, objectView, true);
        } else {
            objectView = (LinearLayout) convertView;
        }

        // Récupérer les View des orderView
        // Pour les Category: une textview seulement
        LinearLayout categoryLayout = (LinearLayout) objectView.findViewById(R.id.catalog_client_category_layout);
        TextView categoryTextView = (TextView) objectView.findViewById(R.id.catalog_client_category_textview);

        // Pour les produits: une image du produit et le nom
        LinearLayout productLayout = (LinearLayout) objectView.findViewById(R.id.catalog_client_product_layout);
        ImageView productImageView = (ImageView) objectView.findViewById(R.id.catalog_client_product_imageview);
        TextView productTextView = (TextView) objectView.findViewById(R.id.catalog_client_product_name_textview);
        // Pour le retour
        TextView textViewBack = (TextView) objectView.findViewById(R.id.client_catalog_back_button);
        

        // Remplissage des CatalogCategory
        if (object instanceof CatalogCategory) {
            categoryLayout.setVisibility(View.VISIBLE);
            categoryTextView.setText(((CatalogCategory)object).getName() );


        } else if (object instanceof ProductType) {
            productLayout.setVisibility(View.VISIBLE);
            productTextView.setText(((ProductType)object).getName() );
            if(changeColor){
                productLayout.setBackgroundColor(myGrey);
                changeColor= !changeColor;
            }
            else{
                productLayout.setBackgroundColor(myBlue);
                changeColor= !changeColor;
            }
          

            // Ajout du listener pour ajouter un produit au panier
            Button buttonAdd = (Button) objectView.findViewById(R.id.button_add);
            buttonAdd.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Basket basket = ((MainActivity) context).basket;
                    basket.getProducts().add(((ProductType)object));
                    Toast toast = Toast.makeText(getContext(), "Product added", Toast.LENGTH_SHORT);
                    toast.show();
                }
            });

            productLayout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    ((MainActivity) context).LaunchProductCardFragment(((ProductType) object));
                }
            });



        } else {
            textViewBack.setVisibility(View.VISIBLE);
            textViewBack.setText((String) object);
        }




        return (objectView);


    }
    
}
